package com.qfedu.dao.impl;

import com.qfedu.dao.CartDao;
import com.qfedu.entity.Cart;
import com.qfedu.entity.Product;
import com.qfedu.utils.BaseDao;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CartDaoImpl extends BaseDao implements CartDao {
    @Override
    public Cart getCartById(int uid, int pid) throws SQLException, InvocationTargetException, IllegalAccessException {
        String sql = "select p.p_name as pname,p.p_id as pid,p.t_id as tid," +
                "p.p_time as ptime,p.p_image as pimage,p_state as pstate," +
                "p.p_info as pinfo, p.p_price as pprice,c.c_id as cid,c.u_id as uid,c.c_count as ccount," +
                "c.c_num as cnum from product p join cart c on p.p_id = c.p_id where" +
                " c.u_id = ? and c.p_id = ?";
        Map<String, Object> query = queryRunner.query(sql, new MapHandler(), uid, pid);
        if (query == null) {
            return null;
        }

        Cart cart = new Cart();
        Product product = new Product();

        BeanUtils.populate(cart,query);
        BeanUtils.populate(product, query);

        cart.setProduct(product);

        return cart;
    }

    @Override
    public int addCart(int uid, int pid, int cnum, BigDecimal ccount) throws SQLException {
        String sql = "insert into cart (u_id,p_id,c_num,c_count) values(?,?,?,?)";
        int num = update(sql, uid,pid,cnum,ccount);
        return num;
    }

    @Override
    public int update(int cnum, BigDecimal ccount, int cid) throws SQLException {
        String sql = "update cart set c_num = ? , c_count = ? where c_id = ?";
        int num = update(sql,cnum,ccount,cid);
        return num;
    }


    @Override
    public List<Cart> getCartListByUid(int uid) throws SQLException, InvocationTargetException, IllegalAccessException {
        String sql = "select p.p_name as pname,p.p_id as pid,p.t_id as tid," +
                "p.p_time as ptime,p.p_image as pimage,p_state as pstate," +
                "p.p_info as pinfo, p.p_price as pprice,c.c_id as cid,c.u_id as uid,c.c_count as ccount," +
                "c.c_num as cnum from product p join cart c on p.p_id = c.p_id where" +
                " c.u_id = ?;";
        List<Cart> cartLsit = new ArrayList<>();
        List<Map<String,Object>> list = queryRunner.query(sql,new MapListHandler(),uid);
        for(Map<String,Object> map:list){
        // 将map 中的数据 封装到cart中
            Cart cart = new Cart();
            Product product = new Product();
            BeanUtils.populate(cart,map);
            BeanUtils.populate(product, map);
            cart.setProduct(product);
            cartLsit.add(cart);
        }
        return cartLsit;

    }

    @Override
    public int delete(int cid) throws SQLException {
        String sql = "delete from cart where c_id = ?";
        int num = update(sql, cid);
        return num;
    }

    @Override
    public int deleteCart(int uid) throws SQLException {
        String sql = "delete from cart where u_id = ?";
        int num = update(sql, uid);
        return num;
    }
}
